package cn.gailvlun.gll.presentation.apartment.room;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.databinding.ActivityBeforeEnterRoomNoticeBinding;
import cn.gailvlun.gll.net.GsonUtil;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.apartment.member.AddMemberReq;
import cn.gailvlun.gll.net.apartment.room.Room;
import cn.gailvlun.gll.presentation.apartment.ApartmentDetailActivity;
import cn.gailvlun.gll.util.DateUtil;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class BeforeEnterRoomNoticeActivity extends BaseActivity {

    public static void openActivity(Context context, Room room) {
        Intent intent = new Intent(context, BeforeEnterRoomNoticeActivity.class);
        intent.putExtra("room", GsonUtil.getGson().toJson(room));
        context.startActivity(intent);
    }

    public static void openActivity(Activity context, Room room, Consumer<Result<Activity>> consumer) {
        Intent intent = new Intent(context, BeforeEnterRoomNoticeActivity.class);
        intent.putExtra("room", GsonUtil.getGson().toJson(room));
        RxActivityResult.on(context)
                .startIntent(intent)
                .subscribe(consumer);
    }

    public static void openActivity(Context context, Room room, boolean isCouple) {
        Intent intent = new Intent(context, BeforeEnterRoomNoticeActivity.class);
        intent.putExtra("room", GsonUtil.getGson().toJson(room));
        intent.putExtra("isCouple", isCouple);
        context.startActivity(intent);
    }

    private ActivityBeforeEnterRoomNoticeBinding mBinding;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_before_enter_room_notice);
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    private Room mRoom;
    private boolean mIsCouple;

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        String roomStr = getIntent().getStringExtra("room");
        mIsCouple = getIntent().getBooleanExtra("isCouple", false);
        if (roomStr != null) {
            mRoom = GsonUtil.getGson().fromJson(roomStr, Room.class);
        }

        String onlyDay = DateUtil.format(DateUtil.ONLY_DAY_PATTERN, mRoom.getStart_datetime());

        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append("为了等待其他房客，活动将在")
                .append(onlyDay)
                .append("号正式开始，")
                .append(onlyDay)
                .append("号晚上20点整，房主会将大家都拉入房间，请稍安勿躁嗷~\n")
                .append("房间开启之后，请尽快进入，超过一定时间，将不得入内；\n")
                .append("在等待过程中，你可以参与app内的其它活动，但请勿随意退出房间；\n")
                .append("若系统检测到账号频繁退出房间，可能会对账号进行违规处理。");

        ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.silverTwo)), 19 + onlyDay.length(),
                26 + onlyDay.length() * 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.silverTwo)), 96 + onlyDay.length() * 2,
                105 + onlyDay.length() * 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        mBinding.tvRule.setText(ssb);
    }

    public class Presenter {
        public void onAgree() {
            HttpMethods.getMemberService()
                    .addMember(new AddMemberReq(mRoom.getId()))
                    .compose(RxUtil.<Member>applyScheduler())
                    .subscribe(new HttpSubscriber<Member>() {
                        @Override
                        protected void onSuccess(Member response) {
                            if (mIsCouple) {
                                InviteFriendActivity.openActivity(mContext, mRoom);
                                finish();
                            } else {
                                ApartmentDetailActivity.openActivity(mContext, mRoom.getId());
                            }
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable Member response, int code) {
                            DialogUtil.showError(mContext, errMsg);
                        }
                    });
        }
    }
}
